package com.wangyuhang.controller;

import com.wangyuhang.vo.Customer;
import com.wangyuhang.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class controller2 {
    /*响应  对象转json*/
    @RequestMapping("/UserResponse")
    @ResponseBody
    public User userpojo(){
        User user=new User();
        user.setName("zhangsan");
        user.setId(20);
//        user.setCustomer(new Customer("lisi",22));
        Customer customer=new Customer();
        customer.setId(22);
        customer.setName("lisi");
        user.setCustomer(customer);
        return user;
    }
    /* 对象集合转json对象*/
    @RequestMapping("/ListResponse")
    @ResponseBody
    public List<User> listpojo(){
        List<User> list=new ArrayList<>();
        User user=new User("zhangsan",32);
        User user1=new User("lisi",22);
        list.add(user);
        list.add(user1);
        return list;
    }

    /*Rest 风格*/
    /*删除*/
    @RequestMapping(value = "/users/{id}" ,method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id){
        System.out.println("use delete"+id);
        return " use delete";
    }
    /*查询*/
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @ResponseBody
    public String find(){
        System.out.println("use find");
        return "method:find";
    }
    /*按id查询*/
    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String find2(@PathVariable Integer id ){
        System.out.println("use findAll"+id);
        return "method:find";
    }

}
